package monster.com.cvh.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;
import monster.com.cvh.R;
import monster.com.cvh.adapter.NoticeAdapter;
import monster.com.cvh.bean.GetNoticeBean;
import monster.com.cvh.bean.NoticeBean;
import monster.com.cvh.fragment.base.PermissionFragment;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: monster
 * @data: 2018/3/29
 * @function: 通知界面
 */
public class NoticeFragment extends PermissionFragment {
    @BindView(R.id.rl_fragment_notice)
    RecyclerView rlFragmentNotice;
    Unbinder unbinder;
    @BindView(R.id.rf_fragment_notice)
    SmartRefreshLayout rfFragmentNotice;
    private ArrayList<NoticeBean> mBeans = new ArrayList<>();
    private int page = 0;
    private NoticeAdapter noticeAdapter;
    private List<GetNoticeBean.DataBean> data;
    private List<GetNoticeBean.DataBean> mDatas = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_notice;
    }

    @Override
    protected void initData() {
        getNotice(page);
        rfFragmentNotice.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(final RefreshLayout refreshlayout) {
                page++;
                getNotice(page);
                noticeAdapter.notifyDataSetChanged();
                refreshlayout.finishLoadmore(500,true);
            }
        });
        rfFragmentNotice.setEnableRefresh(false);

    }
    /**
     * 获取通知
     */
    private void getNotice(int pageNum) {
        OkGo.get(MyConstant.GET_NOTIFICATION_LIST).params("token",SPUtils.getString(getActivity(),"token",""))
                .params("page_index",pageNum).execute(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Gson gson = new Gson();
                GetNoticeBean getNoticeBean = gson.fromJson(s, GetNoticeBean.class);
                data = getNoticeBean.getData();
                if (page == 0) {
                    if (data != null&&data.size() > 0) {
                        mDatas.addAll(data);
                        for (int i = 0; i < data.size(); i++) {
                            boolean is_sys_notification = data.get(i).isIs_sys_notification();
                            if (is_sys_notification) {
                                mBeans.add(new NoticeBean(NoticeBean.RESET_PWD));
                            } else {
                                mBeans.add(new NoticeBean(NoticeBean.ARTICLE));
                            }
                        }
                    } else {
                        showToastShort("无更多数据");
                    }
                    LinearLayoutManager manager = new LinearLayoutManager(getActivity());
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    rlFragmentNotice.setLayoutManager(manager);
                    noticeAdapter = new NoticeAdapter(mBeans, mDatas);
                    rlFragmentNotice.setAdapter(noticeAdapter);
                }else{
                    if (data != null&&data.size() >0){
                        mDatas.addAll(data);
                        for (int i = 0; i < data.size(); i++) {
                            boolean is_sys_notification = data.get(i).isIs_sys_notification();
                            if (is_sys_notification) {
                                mBeans.add(new NoticeBean(NoticeBean.RESET_PWD));
                            } else {
                                mBeans.add(new NoticeBean(NoticeBean.ARTICLE));
                            }
                        }
                    }else {
                        showToastShort("暂无更多通知数据");
                    }
                }
            }
        });
    }




    @Override
    protected void initEvent() {

    }

}
